package gdconf

import (
	"server_cluster/common/logger"
)

// 商店配置表

const (
	ShopBuyLimitTypeNone    = 1 // 无限购
	ShopBuyLimitTypeDay     = 2 // 日限购
	ShopBuyLimitTypeWeek    = 3 // 周限购
	ShopBuyLimitTypeMonth   = 4 // 月限购
	ShopBuyLimitTypeForever = 5 // 永久限购
)

const (
	ShopUnlockTypePlayerLevel = 1 // 玩家等级解锁
)

type ShopConfig struct {
	ShopGoodsId   int32 `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 商品id
	ItemId        int32 `csv:"Itemid,omitempty"`                          // 道具id
	ItemCount     int32 `csv:"Itemnum,omitempty"`                         // 道具数量
	BuyLimitType  int32 `csv:"Limittype,omitempty"`                       // 限购类型
	BuyLimitCount int32 `csv:"Limitnum,omitempty"`                        // 限购数量
	CostItem      *Item `csv:"Costtype,omitempty"`                        // 消耗道具
	UnlockType    int32 `csv:"Locktype,omitempty"`                        // 解锁条件类型
	UnlockParam   int32 `csv:"Locknum,omitempty"`                         // 解锁条件参数
	StartTime     *Time `csv:"Startime,omitempty"`                        // 开始时间
	EndTime       *Time `csv:"Endtime,omitempty"`                         // 结束时间
}

func (s *ShopConfig) TableName() string {
	return "config_shop"
}

func (g *GameDataConfig) saveShopConfig() {
	saveTableToDb[ShopConfig](g.Db, readTable[ShopConfig](g.CsvPathPrefix+"ShopGoods.csv"))
}

func (g *GameDataConfig) loadShopConfig() {
	g.GameDataMaps.ShopConfigMap = make(map[int32]*ShopConfig)
	for _, shopConfig := range loadTableFromDb[ShopConfig](g.Db) {
		g.GameDataMaps.ShopConfigMap[shopConfig.ShopGoodsId] = shopConfig
	}
	logger.Info("ShopConfig Count: %v", len(g.GameDataMaps.ShopConfigMap))
}

func GetShopConfig(shopGoodsId int32) *ShopConfig {
	value, exist := CONF.GameDataMaps.ShopConfigMap[shopGoodsId]
	if !exist {
		logger.Error("[ConfigNotFound] ShopConfig, shopGoodsId: %v", shopGoodsId)
		return nil
	}
	return value
}

func GetShopConfigMap() map[int32]*ShopConfig {
	return CONF.GameDataMaps.ShopConfigMap
}
